package com.qc31.baselib.custom

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.qc31.baselib.R
import com.qc31.baselib.databinding.LayouToolbarBinding

class MToolbar : LinearLayout {
    constructor(context: Context)
            : super(context)

    constructor(context: Context, attrs: AttributeSet?)
            : super(context, attrs) {
        init(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyle: Int)
            : super(context, attrs, defStyle) {
        init(context, attrs)
    }

    private lateinit var binding: LayouToolbarBinding
    fun init(context: Context, attrs: AttributeSet?) {
        binding = LayouToolbarBinding.inflate(LayoutInflater.from(context), this, true)

        val typedArray: TypedArray =
            context.obtainStyledAttributes(attrs, R.styleable.MToolbar)
        val startDrawable = typedArray.getDrawable(R.styleable.MToolbar_mStartIcon)
        val centerTitle = typedArray.getString(R.styleable.MToolbar_mCenterTitle)
        val actionTitle = typedArray.getString(R.styleable.MToolbar_mActionTitle)

        if (startDrawable != null) {
            binding.ivToolbarStart.visibility = View.VISIBLE
            binding.ivToolbarStart.setImageDrawable(startDrawable)
            binding.ivToolbarStart.setOnClickListener {
                if (context is Activity) context.finish()
            }
        }

        if (centerTitle != null) {
            binding.tvToolbarTitle.text = centerTitle
        }
        if (actionTitle != null) {
            binding.tvToolbarAction.visibility = View.VISIBLE
            binding.tvToolbarAction.text = actionTitle
        }

        binding.tvToolbarAction.setOnClickListener {
            callback?.actionOnClick(binding.tvToolbarAction)
        }
        setBackgroundResource(R.color.color_379)
        typedArray.recycle()
    }


    fun setCenterTitle(titleId: Int): MToolbar {
        binding.tvToolbarTitle.setText(titleId)
        return this
    }

    fun setCenterTitle(title: String): MToolbar {
        binding.tvToolbarTitle.text = title
        return this
    }

    fun setActionTitle(titleId: Int): MToolbar {
        binding.tvToolbarAction.setText(titleId)
        binding.tvToolbarAction.visibility = View.VISIBLE
        return this
    }

    fun setActionTitle(title: String): MToolbar {
        binding.tvToolbarAction.text = title
        binding.tvToolbarAction.visibility = View.VISIBLE
        return this
    }

    fun getActionView(): TextView {
        return binding.tvToolbarAction
    }


    private var callback: IToolbarCallback? = null
    fun setCallBack(callback: IToolbarCallback) {
        this.callback = callback
    }

    interface IToolbarCallback {
        fun actionOnClick(action: TextView)
    }


    fun setBackClick(click: View.OnClickListener) {
        binding.ivToolbarStart.setOnClickListener(click)
    }
}